
<?php
/**
 * 判断是否是手机登陆的
 * @return boolean
 */
function isMobile() {
  $mobile = array();
  static $mobilebrowser_list ='Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
  //note 获取手机浏览器
  if(preg_match("/$mobilebrowser_list/i", $_SERVER['HTTP_USER_AGENT'], $mobile)) {
    return true;
  }else{
    if(preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER['HTTP_USER_AGENT'])) {
      return false;
    }else{
      if($_GET['mobile'] === 'yes') {
        return true;
      }else{
        return false;
      }
    }
  }
}
  //多图上传
 function sendpic($img,$address){
       for($i=0;$i<count($img);$i++){
           $newImgType= substr($img[$i],0,strrpos($img[$i],';'));  //图片的类型
           $base=substr($img[$i],strrpos($img[$i],';')+1);
           $a=explode(',',$base);
           $types=explode('/',$newImgType);
           $type=$types[1];
           $filename = rand(0,100000).time().".".$type; 
           $xmlstr=base64_decode($a[1]);
           makeDir($address);
           $file = fopen($address.$filename,"w");//打开文件准备写入  
           fwrite($file,$xmlstr);//写入
           fclose($file);//关闭
           $fileaddress[]='/'.$address.$filename;
       } 
       $info=implode('|',$fileaddress);
       return $info.'|';
    }
     //单图上传
function sendonepic($img,$address){
        $newImgType= substr($img,0,strrpos($img,';'));  //图片的类型
        $base=substr($img,strrpos($img,';')+1);
        $a=explode(',',$base);
        $types=explode('/',$newImgType);
        $type=$types[1];
        $filename = rand(0,100000).time().".".$type; 
        $xmlstr=base64_decode($a[1]);
        makeDir($address);
        $file = fopen($address.$filename,"w");//打开文件准备写入  
        fwrite($file,$xmlstr);//写入
        fclose($file);//关闭
        $fileaddress='/'.$address.$filename;
        return $fileaddress;
    }
//图片缩放
function img_big_min($img,$sin,$w,$h,$address){
      if(!is_array($img)){
         $array_img=explode($sin,$img);
      }else{
         $array_img=$img;
      }
      foreach ($array_img as $key => $value) {
        if(empty($value)){
         unset($array_img[$key]);
        }
      }
      makeDir($address);
      foreach ($array_img as $key => $value) {
        $image = new \Think\Image(); 
        $image->open('./'.$value);
        $address_im=$address.time().rand(1000000,9999999).'.'.jpg;
        $image->thumb($w, $h)->save($address_im); 
        $array_img_min.=substr($address_im,1).'|';
      }

      return $array_img_min;
}    

//图片缩放  一张
function img_big_min_one($img,$sin,$w,$h,$address){
        makeDir($address);
        $image = new \Think\Image(); 
        $image->open('./'.$img);
        $address_im=$address.time().rand(1000000,9999999).'.'.jpg;
        $image->thumb($w, $h)->save($address_im); 
        $array_img_min.=substr($address_im,1);
      return $array_img_min;
} 
//获取城市
 function more_city(){
      $domain=$_SERVER['HTTP_HOST'];
      $city=explode('.', $domain);
      $where['city_domain'] = $city['0'];
      $city_info=M('areas')->field('id,pid,area_name,level')->where($where)->find();
      if(empty($city_info)){
          $ip='peoplehub_discuss';
          $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=3j2s1pCFpoATNL5tcr014rqZbDV8EUh6&ip={$ip}&coor=bd09ll");
          $info = json_decode($content);
          $area_name=$info->{'content'}->{'address_detail'}->{'city'};
          $city_name=str_replace('市','',$area_name);
          $map['area_name'] = array('like','%'.$city_name.'%');
          $city_info=M('areas')->field('id,pid,area_name,level')->where($map)->find();
      }
      return $city_info;
  }

  function format_date($time){
    $t=time()-$time;
    $f=array(
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.'前';
        }
    }
}